package com.kly.user.utils;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Created with IntelliJ IDEA.
 * User: sm
 * Date: 2022/1/3
 * Time: 8:46 下午
 */
@Slf4j
public class LittleOrangeService  {

    private static final Logger LOGGER = LoggerFactory.getLogger(LittleOrangeService.class);


    public static String post(String url, String auth, String requestBody) {


        String[] command = {"curl", "--location", "--request", "POST", url,
                "--header", "accept: application/json",
                "--header", "authentication:" + auth,
                "--header", "content-type: application/json; charset=utf-8",
                "--data-raw", requestBody};
        return execCurl(command);
    }

    /**
     * curl -v -X POST -H "Authorization: Bearer e85aefe121692886acb0b2c6c4bee449" "https://s2s.adjust.com/event?s2s=1&app_token=o7c4dqoa1q0w&event_token=qioyb6&gps_adid=11110111-2345-6789-1122-000000005678"

     */
    public static String postForm(String url, String auth) {

        String[] command = {"curl", "--location", "--request", "GET", url,
                "--header", "accept: */*",
                "--header", "Authorization:Bearer " + auth,
                "--header", "content-type: text/plain; charset=utf-8"};
        return execCurl(command);
    }

    public static String getCsvData(String url, String auth) {


        String[] command = {"curl", "--location", "--request", "POST", url,
                "--header", "accept: text/csv",
                "--header", "authorization: Bearer " + auth};
        return execCurl(command);
    }


    public static String execCurl(String[] cmds) {
        ProcessBuilder process = new ProcessBuilder(cmds);
        Process p;
        try {
            p = process.start();
            BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
            StringBuilder builder = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
//                builder.append(System.getProperty("line.separator"));
            }
            return builder.toString();

        } catch (IOException e) {
            log.error("====请求异常", e);
        }
        return null;
    }
}
